package xiao_jin.fantastic_journey.server.event;

import club.someoneice.cookie.event.SubEvent;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageSources;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.util.math.MathHelper;
import xiao_jin.fantastic_journey.core.handler.LivingEntityEvent;
import xiao_jin.fantastic_journey.core.init.ModEffects;
import xiao_jin.fantastic_journey.core.init.ModFluids;

public final class EntityEvent {
    @SubEvent
    public static void onEntitySwim(LivingEntityEvent.LivingSwimUpawrdEvent event) {
        if (event.fluid.isOf(ModFluids.OIL) || event.fluid.isOf(ModFluids.OIL_FLOWING))
            event.setJumpingCooldown(40);
    }

    @SubEvent
    public static void onEntityJumping(LivingEntityEvent.LivingJumpingEvent event) {
        if (event.entityLiving.hasStatusEffect(ModEffects.OSTEOPOROSIS)) {
            event.setCancel();
        }
    }

    @SubEvent
    public static void onEntityDamage(LivingEntityEvent.LivingHurtEvent event) {
        LivingEntity entity = event.entityLiving;

        if (entity.hasStatusEffect(ModEffects.OSTEOPOROSIS)) {
            StatusEffectInstance effect = entity.getStatusEffect(ModEffects.OSTEOPOROSIS);
            assert effect != null;
            if (event.source == entity.getDamageSources().fall()) {
                event.setAmount(MathHelper.floor(event.getAmount() * (0.5 + effect.getAmplifier())));
            }
        }
    }
}
